home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / ViePratique / ArchiFacile / ArchiFacileSetup.exe / {app} / nw.pak / Unnamed File 000003.txt < prev    next >
Text File  |  2014-10-14  |  5KB  |  144 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  6. // Use of this source code is governed by a BSD-style license that can be
  7. // found in the LICENSE file.
  8.  
  9. /**
  10.  * @fileoverview This is a simple template engine inspired by JsTemplates
  11.  * optimized for i18n.
  12.  *
  13.  * It currently supports three handlers:
  14.  *
  15.  *   * i18n-content which sets the textContent of the element.
  16.  *
  17.  *     <span i18n-content="myContent"></span>
  18.  *
  19.  *   * i18n-options which generates <option> elements for a <select>.
  20.  *
  21.  *     <select i18n-options="myOptionList"></select>
  22.  *
  23.  *   * i18n-values is a list of attribute-value or property-value pairs.
  24.  *     Properties are prefixed with a '.' and can contain nested properties.
  25.  *
  26.  *     <span i18n-values="title:myTitle;.style.fontSize:fontSize"></span>
  27.  *
  28.  * This file is a copy of i18n_template.js, with minor tweaks to support using
  29.  * load_time_data.js. It should replace i18n_template.js eventually.
  30.  */
  31.  
  32. var i18nTemplate = (function() {
  33.   /**
  34.    * This provides the handlers for the templating engine. The key is used as
  35.    * the attribute name and the value is the function that gets called for every
  36.    * single node that has this attribute.
  37.    * @type {Object}
  38.    */
  39.   var handlers = {
  40.     /**
  41.      * This handler sets the textContent of the element.
  42.      * @param {HTMLElement} element The node to modify.
  43.      * @param {string} key The name of the value in the dictionary.
  44.      * @param {LoadTimeData} dictionary The dictionary of strings to draw from.
  45.      */
  46.     'i18n-content': function(element, key, dictionary) {
  47.       element.textContent = dictionary.getString(key);
  48.     },
  49.  
  50.     /**
  51.      * This handler adds options to a <select> element.
  52.      * @param {HTMLElement} select The node to modify.
  53.      * @param {string} key The name of the value in the dictionary. It should
  54.      *     identify an array of values to initialize an <option>. Each value,
  55.      *     if a pair, represents [content, value]. Otherwise, it should be a
  56.      *     content string with no value.
  57.      * @param {LoadTimeData} dictionary The dictionary of strings to draw from.
  58.      */
  59.     'i18n-options': function(select, key, dictionary) {
  60.       var options = dictionary.getValue(key);
  61.       options.forEach(function(optionData) {
  62.         var option = typeof optionData == 'string' ?
  63.             new Option(optionData) :
  64.             new Option(optionData[1], optionData[0]);
  65.         select.appendChild(option);
  66.       });
  67.     },
  68.  
  69.     /**
  70.      * This is used to set HTML attributes and DOM properties. The syntax is:
  71.      *   attributename:key;
  72.      *   .domProperty:key;
  73.      *   .nested.dom.property:key
  74.      * @param {HTMLElement} element The node to modify.
  75.      * @param {string} attributeAndKeys The path of the attribute to modify
  76.      *     followed by a colon, and the name of the value in the dictionary.
  77.      *     Multiple attribute/key pairs may be separated by semicolons.
  78.      * @param {LoadTimeData} dictionary The dictionary of strings to draw from.
  79.      */
  80.     'i18n-values': function(element, attributeAndKeys, dictionary) {
  81.       var parts = attributeAndKeys.replace(/\s/g, '').split(/;/);
  82.       parts.forEach(function(part) {
  83.         if (!part)
  84.           return;
  85.  
  86.         var attributeAndKeyPair = part.match(/^([^:]+):(.+)$/);
  87.         if (!attributeAndKeyPair)
  88.           throw new Error('malformed i18n-values: ' + attributeAndKeys);
  89.  
  90.         var propName = attributeAndKeyPair[1];
  91.         var propExpr = attributeAndKeyPair[2];
  92.  
  93.         var value = dictionary.getValue(propExpr);
  94.  
  95.         // Allow a property of the form '.foo.bar' to assign a value into
  96.         // element.foo.bar.
  97.         if (propName[0] == '.') {
  98.           var path = propName.slice(1).split('.');
  99.           var targetObject = element;
  100.           while (targetObject && path.length > 1) {
  101.             targetObject = targetObject[path.shift()];
  102.           }
  103.           if (targetObject) {
  104.             targetObject[path] = value;
  105.             // In case we set innerHTML (ignoring others) we need to
  106.             // recursively check the content.
  107.             if (path == 'innerHTML')
  108.               process(element, dictionary);
  109.           }
  110.         } else {
  111.           element.setAttribute(propName, value);
  112.         }
  113.       });
  114.     }
  115.   };
  116.  
  117.   var attributeNames = Object.keys(handlers);
  118.   var selector = '[' + attributeNames.join('],[') + ']';
  119.  
  120.   /**
  121.    * Processes a DOM tree with the {@code dictionary} map.
  122.    * @param {HTMLElement} node The root of the DOM tree to process.
  123.    * @param {LoadTimeData} dictionary The dictionary to draw from.
  124.    */
  125.   function process(node, dictionary) {
  126.     var elements = node.querySelectorAll(selector);
  127.     for (var element, i = 0; element = elements[i]; i++) {
  128.       for (var j = 0; j < attributeNames.length; j++) {
  129.         var name = attributeNames[j];
  130.         var attribute = element.getAttribute(name);
  131.         if (attribute != null)
  132.           handlers[name](element, attribute, dictionary);
  133.       }
  134.     }
  135.   }
  136.  
  137.   return {
  138.     process: process
  139.   };
  140. }());
  141.  
  142.  
  143. i18nTemplate.process(document, loadTimeData);
  144.